package com.hnkywl.sts.action.tjbb;

import com.hnkywl.sts.dao.organ.GsjgDao;
import com.hnkywl.sts.dao.tjbb.GyswlDao;
import com.hnkywl.sts.entity.organ.Gsjg;
import com.hnkywl.sts.service.tjbb.GyswlService;
import com.hnkywl.sts.util.POIUtil;
import com.hnkywl.sts.util.StringFormat;
import net.ruixin.web.struts2.SimpleActionSupport;
import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.hssf.util.Region;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.springframework.beans.factory.annotation.Autowired;

import java.io.UnsupportedEncodingException;
import java.math.BigDecimal;
import java.net.URLDecoder;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * User: SDE09
 * Date: 13-9-28
 * Time: 下午5:06
 * To change this template use File | Settings | File Templates.
 */
@Namespace("/tjbb/gyswl")
public class GyswlAction extends SimpleActionSupport {
    private GyswlService gyswlService;
    private GyswlDao gyswlDao;
    private GsjgDao gsjgDao;

    private Integer gsid;
    private Integer deptid;
    private Date enddt;
    private String custname;
    private int page;
    private int rows;
    private Integer tc_flag;
    private Date begindt;
    private Integer type;
    private Gsjg gys;


    @Action("gyswl")
    public String gyswl() {

        /*     Calendar calendar = Calendar.getInstance();
        //得到月末
      calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
      enddt = calendar.getTime();*/
        //enddt = new Date();
        if (enddt == null) {
            enddt = new Date();
        }
        return SUCCESS;
    }

    @Action("gyswlList")
    public void gyswlList() {
        Calendar calendar = Calendar.getInstance();
        if (enddt == null) {
            //得到月末
            calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
            enddt = calendar.getTime();
            // enddt = new Date();
        }
        if (tc_flag == null) {
            tc_flag = 1;
        }

        String json = gyswlService.gyswlList(gsid, deptid, enddt, custname, page, rows, tc_flag);
        renderJson(json);
    }

    //供应商往来对账跳转
    @Action("gyswldz")
    public String gyswlDz() {
        Calendar startCal = Calendar.getInstance();
        startCal.setTime(enddt);
        startCal.set(Calendar.DAY_OF_MONTH, 1);
        begindt = startCal.getTime();
        try {
            custname = URLDecoder.decode(custname, "UTF-8");
            gys = gsjgDao.findGsjgFromQc(custname);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        }
        return SUCCESS;
    }

    //供应商往来对账调用集合
    @Action("gyswldzlist")
    public void gyswlDzlist() {
        renderJson(gyswlService.gyswlDz(gsid, deptid, begindt, enddt, custname, type, page, rows));
    }


    //采购未结算金额
    @Action("cgwjsje")
    public String cgwjsje() {
        Calendar calendar = Calendar.getInstance();
        if (enddt == null) {
            //得到月末
            calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
            enddt = calendar.getTime();
            // enddt = new Date();
        }
        try {
            custname = URLDecoder.decode(custname, "UTF-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        }
        return SUCCESS;
    }

    @Action("cgwjsjelist")
    public void cgwjsjelist() {
        renderJson(gyswlService.cgwjsje(gsid, enddt, custname));
    }

    @Action("gyswl_export")
    public String gyswlExport() {
        Map<String, List<Map>> gyswlInfo = gyswlDao.gyswlListForExport(gsid, deptid, enddt, custname, 1, 9999, tc_flag);
        HSSFWorkbook excel = new HSSFWorkbook();
        POIUtil poiUtil = new POIUtil(excel);
        HSSFCellStyle cellStyleCenter = poiUtil.createContent3HSSFCellStyle(excel);
        HSSFCellStyle cellStyleLeft = poiUtil.createContent4HSSFCellStyle(excel);
        HSSFCellStyle headerCelleStyle = poiUtil.createHeaderHSSFCellStyle(excel);
        HSSFCellStyle titleStyle = poiUtil.createTitleHSSFCellStyle(excel);
        HSSFSheet sheet = excel.createSheet();
        excel.setSheetName(0, "供应商往来", HSSFWorkbook.ENCODING_UTF_16);
        sheet.setDefaultColumnWidth((short) 15);
        sheet.setColumnWidth((short) 0, (short) (3 * 512));
        sheet.setColumnWidth((short) 1, (short) (12 * 512));
        sheet.setColumnWidth((short) 2, (short) (12 * 512));
        sheet.setColumnWidth((short) 3, (short) (12 * 512));
        sheet.setColumnWidth((short) 4, (short) (12 * 512));
        sheet.setColumnWidth((short) 5, (short) (12 * 512));
        sheet.setColumnWidth((short) 6, (short) (12 * 512));
        sheet.setColumnWidth((short) 7, (short) (12 * 512));
        sheet.setColumnWidth((short) 8, (short) (12 * 512));
        sheet.setColumnWidth((short) 9, (short) (12 * 512));


        HSSFRow row0 = sheet.createRow((int) 0);
        row0.setHeightInPoints(36);
        HSSFCell titleCell = row0.createCell((short) 0);
        titleCell.setCellStyle(titleStyle);
        titleCell.setEncoding(HSSFCell.ENCODING_UTF_16);
        titleCell.setCellValue("供应商往来导出表");
        sheet.addMergedRegion(new Region(0, (short) 0, 0, (short) 9));

        HSSFRow row1 = sheet.createRow((int) 1);
        row1.setHeightInPoints(21);
        HSSFCell xh = row1.createCell((short) 0);
        xh.setCellStyle(headerCelleStyle);
        xh.setEncoding(HSSFCell.ENCODING_UTF_16);
        xh.setCellValue("序号");
        HSSFCell gsmc = row1.createCell((short) 1);
        gsmc.setCellStyle(headerCelleStyle);
        gsmc.setEncoding(HSSFCell.ENCODING_UTF_16);
        gsmc.setCellValue("公司名称");
        HSSFCell gysmc = row1.createCell((short) 2);
        gysmc.setCellStyle(headerCelleStyle);
        gysmc.setEncoding(HSSFCell.ENCODING_UTF_16);
        gysmc.setCellValue("供应商名称");
        HSSFCell ywyufk = row1.createCell((short) 3);
        ywyufk.setCellStyle(headerCelleStyle);
        ywyufk.setEncoding(HSSFCell.ENCODING_UTF_16);
        ywyufk.setCellValue("业务预付款");
        HSSFCell ywyingfk = row1.createCell((short) 4);
        ywyingfk.setCellStyle(headerCelleStyle);
        ywyingfk.setEncoding(HSSFCell.ENCODING_UTF_16);
        ywyingfk.setCellValue("业务应付款");
        HSSFCell cgwsjsl = row1.createCell((short) 5);
        cgwsjsl.setCellStyle(headerCelleStyle);
        cgwsjsl.setEncoding(HSSFCell.ENCODING_UTF_16);
        cgwsjsl.setCellValue("采购未结算数量 ");
        HSSFCell cgwjsje = row1.createCell((short) 6);
        cgwjsje.setCellStyle(headerCelleStyle);
        cgwjsje.setEncoding(HSSFCell.ENCODING_UTF_16);
        cgwjsje.setCellValue("采购未结算金额");
        HSSFCell wlwjsje = row1.createCell((short) 7);
        wlwjsje.setCellStyle(headerCelleStyle);
        wlwjsje.setEncoding(HSSFCell.ENCODING_UTF_16);
        wlwjsje.setCellValue("物流未结算金额");
        HSSFCell cwyufk = row1.createCell((short) 8);
        cwyufk.setCellStyle(headerCelleStyle);
        cwyufk.setEncoding(HSSFCell.ENCODING_UTF_16);
        cwyufk.setCellValue("财务预付款");
        HSSFCell cwyingfk = row1.createCell((short) 9);
        cwyingfk.setCellStyle(headerCelleStyle);
        cwyingfk.setEncoding(HSSFCell.ENCODING_UTF_16);
        cwyingfk.setCellValue("财务应付款");


        int i = 2;
        for (; i < gyswlInfo.get("rows").size() + 2; i++) {
            Map<String, Object> gyswlRow = gyswlInfo.get("rows").get(i - 2);
            HSSFRow row = sheet.createRow(i);
            row.setHeightInPoints(19);
            row = sheet.createRow(i);
            xh = row.createCell((short) 0);
            xh.setCellStyle(cellStyleCenter);
            xh.setEncoding(HSSFCell.ENCODING_UTF_16);
            xh.setCellValue(Integer.toString(i - 1));

            gsmc = row.createCell((short) 1);
            gsmc.setCellStyle(cellStyleCenter);
            gsmc.setEncoding(HSSFCell.ENCODING_UTF_16);
            if (null != gyswlRow.get("gs_name")) {
                gsmc.setCellValue(gyswlRow.get("gs_name").toString());
            }

            gysmc = row.createCell((short) 2);
            gysmc.setCellStyle(cellStyleCenter);
            gysmc.setEncoding(HSSFCell.ENCODING_UTF_16);
            if (null != gyswlRow.get("cust_name")) {
                gysmc.setCellValue(gyswlRow.get("cust_name").toString());
            }


            ywyufk = row.createCell((short) 3);
            ywyufk.setCellStyle(cellStyleCenter);
            ywyufk.setEncoding(HSSFCell.ENCODING_UTF_16);
            if (null != gyswlRow.get("yw_yufk")) {
                ywyufk.setCellType(HSSFCell.CELL_TYPE_NUMERIC);
                ywyufk.setCellValue(((BigDecimal) gyswlRow.get("yw_yufk")).doubleValue());
            }


            ywyingfk = row.createCell((short) 4);
            ywyingfk.setCellStyle(cellStyleCenter);
            ywyingfk.setEncoding(HSSFCell.ENCODING_UTF_16);
            if (null != gyswlRow.get("yw_yingfk")) {
                ywyingfk.setCellType(HSSFCell.CELL_TYPE_NUMERIC);
                ywyingfk.setCellValue(((BigDecimal) gyswlRow.get("yw_yingfk")).doubleValue());
            }


            cgwsjsl = row.createCell((short) 5);
            cgwsjsl.setCellStyle(cellStyleCenter);
            cgwsjsl.setEncoding(HSSFCell.ENCODING_UTF_16);
            if (null != gyswlRow.get("cgwjs_sl")) {
                cgwsjsl.setCellType(HSSFCell.CELL_TYPE_NUMERIC);
                cgwsjsl.setCellValue(((BigDecimal) gyswlRow.get("cgwjs_sl")).doubleValue());
            }

            cgwjsje = row.createCell((short) 6);
            cgwjsje.setCellStyle(cellStyleCenter);
            cgwjsje.setEncoding(HSSFCell.ENCODING_UTF_16);
            if (null != gyswlRow.get("cgwjs_je")) {
                cgwjsje.setCellType(HSSFCell.CELL_TYPE_NUMERIC);
                cgwjsje.setCellValue(((BigDecimal) gyswlRow.get("cgwjs_je")).doubleValue());
            }

            wlwjsje = row.createCell((short) 7);
            wlwjsje.setCellStyle(cellStyleCenter);
            wlwjsje.setEncoding(HSSFCell.ENCODING_UTF_16);
            if (null != gyswlRow.get("wlwjs_je")) {
                wlwjsje.setCellType(HSSFCell.CELL_TYPE_NUMERIC);
                wlwjsje.setCellValue(((BigDecimal) gyswlRow.get("wlwjs_je")).doubleValue());
            }

            cwyufk = row.createCell((short) 8);
            cwyufk.setCellStyle(cellStyleCenter);
            cwyufk.setEncoding(HSSFCell.ENCODING_UTF_16);
            if (null != gyswlRow.get("cw_yufk")) {
                cwyufk.setCellType(HSSFCell.CELL_TYPE_NUMERIC);
                cwyufk.setCellValue(((BigDecimal) gyswlRow.get("cw_yufk")).doubleValue());
            }


            cwyingfk = row.createCell((short) 9);
            cwyingfk.setCellStyle(cellStyleCenter);
            cwyingfk.setEncoding(HSSFCell.ENCODING_UTF_16);
            if (null != gyswlRow.get("cw_yingfk")) {
                cwyingfk.setCellType(HSSFCell.CELL_TYPE_NUMERIC);
                cwyingfk.setCellValue(((BigDecimal) gyswlRow.get("cw_yingfk")).doubleValue());
            }
        }

        HSSFRow rowFooter = sheet.createRow(i);
        rowFooter.setHeightInPoints(21);
        HSSFCell footerCell = rowFooter.createCell((short) 0);
        footerCell.setCellStyle(titleStyle);
        footerCell.setEncoding(HSSFCell.ENCODING_UTF_16);
        Map<String, Object> gyswlFooter = gyswlInfo.get("footer").get(0);
        String footer = StringFormat.format("合计：业务预付款：{0}，业务应付款：{1}，采购未结算数量{2}，采购未结算金额：{3}，物流未结算金额：{4}，财务预付款{5}，财务应付款：{6}", gyswlFooter.get("yw_yufk").toString()
                , gyswlFooter.get("yw_yingfk").toString(), gyswlFooter.get("cgwjs_sl").toString(), gyswlFooter.get("cgwjs_je").toString(), gyswlFooter.get("wlwjs_je").toString(), gyswlFooter.get("cw_yufk").toString(), gyswlFooter.get("cw_yingfk").toString());
        footerCell.setCellValue(footer);
        sheet.addMergedRegion(new Region(i, (short) 0, i, (short) 15));

        try {
            SimpleDateFormat dateformat = new SimpleDateFormat("yyyyMMdd");
            String filename = StringFormat.format("供应商往来_{0}", dateformat.format(new Date()));
            response.setContentType("application/x-download");
            String filedisplay = new String((filename + ".xls").getBytes("gb2312"), "iso8859-1");
            response.addHeader("Content-Disposition", "attachment;filename=" + filedisplay);
            excel.write(response.getOutputStream());
            response.getOutputStream().close();
        } catch (Exception e) {
            e.printStackTrace();
            renderText("位置异常，导出失败" + e.getMessage());
        }
        return null;
    }

    //==============getter====setter=================


    public Integer getType() {
        return type;
    }

    public void setType(Integer type) {
        this.type = type;
    }

    public Date getBegindt() {
        return begindt;
    }

    public void setBegindt(Date begindt) {
        this.begindt = begindt;
    }

    public Integer getGsid() {
        return gsid;
    }

    public void setGsid(Integer gsid) {
        this.gsid = gsid;
    }

    public Integer getDeptid() {
        return deptid;
    }

    public void setDeptid(Integer deptid) {
        this.deptid = deptid;
    }

    public Date getEnddt() {
        return enddt;
    }

    public void setEnddt(Date enddt) {
        this.enddt = enddt;
    }

    public String getCustname() {
        return custname;
    }

    public void setCustname(String custname) {
        this.custname = custname;
    }

    public int getPage() {
        return page;
    }

    public void setPage(int page) {
        this.page = page;
    }

    public Gsjg getGys() {
        return gys;
    }

    public void setGys(Gsjg gys) {
        this.gys = gys;
    }

    public int getRows() {
        return rows;
    }

    public void setRows(int rows) {
        this.rows = rows;
    }

    public Integer getTc_flag() {
        return tc_flag;
    }

    public void setTc_flag(Integer tc_flag) {
        this.tc_flag = tc_flag;
    }

    @Autowired
    public void setGyswlService(GyswlService gyswlService) {
        this.gyswlService = gyswlService;
    }

    @Autowired
    public void setGyswlDao(GyswlDao gyswlDao) {
        this.gyswlDao = gyswlDao;
    }

    @Autowired
    public void setGsjgDao(GsjgDao gsjgDao) {
        this.gsjgDao = gsjgDao;
    }
}
